<template>
  <div class="login-form">
    <h1>用户登录</h1>
    <el-form
      ref="loginForm"
      :model="loginForm"
      size="small"
      :rules="rules"
      class="demo-form-inline"
    >
      <el-form-item prop="username">
        <el-input
          v-model="loginForm.username"
          prefix-icon="el-icon-user-solid"
          placeholder="请输入用户名"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          prefix-icon="el-icon-lock"
          placeholder="请输入密码"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button style="width: 100%" type="primary" @click="login"
          >登录</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    const delUsername = (rule, value, cb) => {
      const reg = /^[a-zA-Z0-9_-]{4,16}$/;
      if (!reg.test(value)) {
        return cb(new Error("请输入4到16位（字母，数字，下划线，减号）"));
      }
      cb();
    };
    return {
      loginForm: {
        username: "admin",
        password: "123456",
      },
      rules: {
        username: [
          {
            trigger: ["blur", "change"],
            validator: delUsername,
          },
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: ["blur", "change"],
          },
        ],
      },
    };
  },
  methods: {
    login() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.$emit("submit-validate", this.loginForm);
        }
      });
    },
  },
};
</script>
<style></style>
